package org.iabako.client.ui.form.business.sale;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigEnum;
import org.iabako.client.config.page.PageTagItemEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.form.business.client.ClientForm;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.template.PopupTagItem;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.client.ui.page.business.sale.SaleSearch;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.business.Client;
import org.iabako.shared.entity.business.InvoiceRecipient;
import org.iabako.shared.entity.business.Sale;
import org.iabako.shared.entity.enumeration.TrackingType;
import org.iabako.shared.iservice.business.SaleServiceAsync;

import java.util.List;

/**
 * Created by jose on 26/08/14.
 */
public abstract class SaleModifyForm extends AbstractForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, SaleModifyForm> {
    }

    protected MyUiBinder binderSalesForm = GWT.create(MyUiBinder.class);

    protected final SaleServiceAsync saleServiceAsync = SaleServiceAsync.Util.getInstance();

    @UiField
    SaleInformationForm saleInformationForm;
    @UiField
    public SalePaymentsForm salePaymentsForm;
    @UiField
    public SaleInvoiceForm saleInvoiceForm;

    protected SaleModifyForm(PageConfigEnum pageConfigEnum, String explanationMenuLabel) {

        setForm(binderSalesForm, pageConfigEnum, explanationMenuLabel);

        for (final PopupTagItem popupTagItem : popup.popupTagItems.keySet()) {
            switch (popupTagItem.pageTagItemEnum) {
                case SaleInformation:
                    popup.popupTagItems.put(popupTagItem, saleInformationForm);
                    break;
                case SalePayment:
                    popup.popupTagItems.put(popupTagItem, salePaymentsForm);
                    break;
                case SaleInvoice:
                    popup.popupTagItems.put(popupTagItem, saleInvoiceForm);
                    break;
            }
        }
        popup.setSelectedTag(PageTagItemEnum.SalePayment);
    }

    protected void setSale(Sale sale) {
        entity = sale;
        saleInformationForm.setSale(sale);
        salePaymentsForm.setSale(sale, saleInvoiceForm);
        saleInvoiceForm.setParentForm(this);

        enableAddLinkAction(saleServiceAsync);
    }

    public void setClient(Client client) {
        ((Sale) entity).setClient(client);
    }

    @Override
    protected List<IField> getMandatoryFields() {
        return saleInformationForm.getMandatoryFields();
    }

    @Override
    protected AbstractEntity getEntity() {

        Sale sale = (Sale) saleInformationForm.getEntity();
        InvoiceRecipient invoiceRecipient = saleInvoiceForm.getInvoiceRecipient();
        sale.setInvoiceRecipient(invoiceRecipient);

        return sale;
    }

    @Override
    public boolean validate() {

        if (!uiValidation()) {
            return false;
        }
        Sale sale = (Sale) serverValidation();
        if (sale == null) {
            return false;
        }

        saleServiceAsync.save(sale, new IabakoAsyncCallback<Sale>() {
            public void onSuccess(Sale result) {

                ValidationMessage validationMessage;

                if (parentForm != null) {
                    validationMessage = ((ClientForm) parentForm).clientSalesForm.messages;
                } else {
                    validationMessage = ValidationMessage.getFromRootPage();
                }

                popup.handleClickClose();
                validationMessage.showSuccessMessage("validation_sale_form_modify_success");

                if (NavigationController.getInstance().getRootPage().pageContentComposite instanceof SaleSearch) {
                    //If rootPage is SaleSearch, we have to refresh search
                    ((SaleSearch) NavigationController.getInstance().
                            getRootPage().pageContentComposite).saleSearchForm.searchForm.search(false);
                }

                AuthenticateUserProvider.getTrackingPanel().refresh(TrackingType.saleModify);

                super.onSuccess(result);
            }
        });
        return true;
    }
}
